<?php

namespace app\admin\logic;

use ReflectionClass;
use ReflectionMethod;
use think\Config;


class Menus
{

    protected $model = null;
    public static $action;

    public function execute()
    {
//        $this->model = new AuthRule();
        $adminPath = dirname(__DIR__) . DS;
//        $this->model->destroy([]);
        $controllerDir = $adminPath . 'controller' . DS;
        // 扫描新的节点信息并导入
        $treelist = $this->import($this->scandir($controllerDir));
        return $treelist;
    }

    /**
     * 递归扫描文件夹
     * @param string $dir
     * @return array
     */
    public function scandir($dir)
    {
        $result = [];
        $cdir = scandir($dir);
        foreach ($cdir as $value) {
            if (!in_array($value, array(".", ".."))) {
                if (is_dir($dir . DS . $value)) {
                    $result[$value] = $this->scandir($dir . DS . $value);
                } else {
                    $result[] = $value;
                }
            }
        }
        return $result;
    }

    /**
     * 导入规则节点
     * @param array $dirarr
     * @param array $parentdir
     * @return array
     */
    public function import($dirarr, $parentdir = [])
    {
        $menuarr = [];
        foreach ($dirarr as $k => $v) {
            if (is_array($v)) {
                //当前是文件夹
                $nowparentdir = array_merge($parentdir, [$k]);
                $this->import($v, $nowparentdir);
            } else {
                //只匹配PHP文件
                if (!preg_match('/^(\w+)\.php$/', $v, $matchone)) {
                    continue;
                }
                //导入文件
                $controller = ($parentdir ? implode('/', $parentdir) . '/' : '') . $matchone[1];
                $relut = $this->importRule($controller);
                if (!is_null($relut)) {
                    $menuarr[] = $relut;
                }
            }
        }

        return $menuarr;
    }

    /**
     *
     * @param $controller
     * @return array
     * @author: Huang
     */
    public function importRule($controller)
    {
        $controllerArr = explode('/', $controller);
        end($controllerArr);
        $key = key($controllerArr);
        $controllerArr[$key] = ucfirst($controllerArr[$key]);
        $classSuffix = Config::get('controller_suffix') ? ucfirst(Config::get('url_controller_layer')) : '';
        $className = "app\\admin\\controller\\" . implode("\\", $controllerArr) . $classSuffix;
        //反射机制调用类的注释和方法名
        $reflector = new ReflectionClass($className);
        //只匹配公共的方法
        $methods = $reflector->getMethods(ReflectionMethod::IS_PUBLIC);
        $classComment = $reflector->getDocComment();
        //忽略的类
        if (stripos($classComment, "@internal") !== FALSE) {
            return;
        }
//        preg_match_all('#(@.*?)\n#s', $classComment, $annotations);
//        $controllerIcon = 'fa fa-circle-o';
//        $controllerRemark = '';
        //判断注释中是否设置了icon值
//        if (isset($annotations[1])) {
//            foreach ($annotations[1] as $tag) {
//                if (stripos($tag, '@icon') !== FALSE) {
//                    $controllerIcon = substr($tag, stripos($tag, ' ') + 1);
//                }
//                if (stripos($tag, '@remark') !== FALSE) {
//                    $controllerRemark = substr($tag, stripos($tag, ' ') + 1);
//                }
//            }
//        }
//        //过滤掉其它字符
//        $controllerTitle = trim(preg_replace(array('/^\/\*\*(.*)[\n\r\t]/u', '/[\s]+\*\//u', '/\*\s@(.*)/u', '/[\s|\*]+/u'), '', $classComment));
//        //先定入菜单的数据
//        $pid = 0;
        foreach ($controllerArr as $k => $v) {
            $key = $k + 1;
            $name = strtolower(implode('/', array_slice($controllerArr, 0, $key)));
//            $title = (!isset($controllerArr[$key]) ? $controllerTitle : '');
//            $icon = (!isset($controllerArr[$key]) ? $controllerIcon : 'fa fa-list');
//            $remark = (!isset($controllerArr[$key]) ? $controllerRemark : '');
//            $title = $title ? $title : $v;
        }
        $ruleArr = [];
        foreach ($methods as $m => $n) {
            //过滤特殊的类
            if (substr($n->name, 0, 2) == '__' || $n->name == '_initialize') {
                continue;
            }
            //只匹配符合的方法
            if (!preg_match('/^(\w+)' . Config::get('action_suffix') . '/', $n->name, $matchtwo)) {
                unset($methods[$m]);
                continue;
            }
            $comment = $reflector->getMethod($n->name)->getDocComment();
            preg_match_all('#(@.*?)\n#s', $comment, $annotations);
            $icon = 'fa-circle-o';
            $title = '';
            //判断注释中是否设置了icon值
            if (isset($annotations[1])) {
                foreach ($annotations[1] as $tag) {
                    if (stripos($tag, '@icon') !== FALSE) {
                        $icon = substr($tag, stripos($tag, ' ') + 1);
                    }
                    if (stripos($tag, '@remark') !== FALSE) {
                        $title = substr($tag, stripos($tag, ' ') + 1);
                    }
                }
            }
            //忽略的方法
            if (stripos($comment, "@internal") !== FALSE) {
                continue;
            }
            //过滤掉其它字符
            $comment = preg_replace(array('/^\/\*\*(.*)[\n\r\t]/u', '/[\s]+\*\//u', '/\*\s@(.*)/u', '/[\s|\*]+/u'), '', $comment);
            $title = $comment ? $comment : ucfirst($n->name);
            $ruleArr[] = array('name' => 'admin/' . $name . "/" . strtolower($n->name), 'css' => trim($icon), 'title' => $title, 'description' => $title);
        }
        return $ruleArr;
    }

    //获取主键
    protected function getAuthRulePK($name)
    {
        if (!empty($name)) {
            return $this->model
                ->where('name', $name)
                ->value('id');

        }
    }

}
